#include <stdio.h>
#include <string.h>
main()
{
	int mode1(),mode2(),mode3();
	char choice;
	printf("请选择模式（选1加密，选2解密，选3破解）：");
	choice=getchar();
	switch(choice)
	{
		case'1':mode1();break;
		case'2':mode2();break;
		case'3':mode3();break;
		default:putchar('\a');
	} 
	return 0;
}


int mode1()
{
	int i,key;
    char c[1000],s; 
    printf("请输入明文:");
    getchar(); 
    gets(c);
	printf("请输入密钥:");
    scanf("%d",&key);
    printf("输出密文是:");
    for(i=0;i<strlen(c);i++)
    {
        s=c[i];
        if(s>='a'&&s<='z')
        {
            s=c[i]+key;
            if(s<='z')
            {
                printf("%c",s);
            }
            else
            {
                s=c[i]+key-26;
                printf("%c",s);
            }
        }
        else if(s>='A'&&s<='Z')
        {
            s=c[i]+key;
            if(s<='Z')
            {
                printf("%c",s);
            }
            else
            {
                s=c[i]+key-26;
                printf("%c",s);
            }
        }
        else if(s<65||s>122)
        {
            s=c[i];
            printf("%c",s);
        }
    }
    return 0;
}





int mode2()
{
    int i,key;
    char c[1000],s; 
    printf("请输入密文:");
    getchar(); 
    gets(c);
	printf("请输入密钥:");
    scanf("%d",&key);
    printf("输出明文是:");
    for(i=0;i<strlen(c);i++)
    {
        s=c[i];
        if(s>='a'&&s<='z')
        {
            s=c[i]-key;
            if(s>='a')
            {
                printf("%c",s);
            }
            else
            {
                s=c[i]-key+26;
                printf("%c",s);
            }
        }
        else if(s>='A'&&s<='Z')
        {
            s=c[i]-key;
            if(s>='A')
            {
                printf("%c",s);
            }
            else
            {
                s=c[i]-key+26;
                printf("%c",s);
            }
        }
        else if(s<65||s>122)
        {
            s=c[i];
            printf("%c",s);
        }
    }
    return 0;
}





int mode3()
{
	int i,key=1;
	char c[1000],s;
	printf("请输入密文：");
	getchar(); 
	gets(c);
	do
	{
		printf("\n当密钥为%d时:",key);
		for(i=0;i<strlen(c);i++)
        {
            s=c[i];
            if(s>='a'&&s<='z')
            {
                s=c[i]-key;
                if(s>='a')
                {
                    printf("%c",s);
                }  
                else
                {
                    s=c[i]-key+26;
                    printf("%c",s);
                }
            }
            else if(s>='A'&&s<='Z')
            {
                s=c[i]-key;
                if(s>='A')
                {
                    printf("%c",s);
                }
                else
                {
                    s=c[i]-key+26;
                    printf("%c",s);
                }
            }
            else if(s<65||s>122)
            {
                s=c[i];
                printf("%c",s);
            }
        }
        key+=1;
	}
	while(key<=26);
	return 0;
}
